from pathlib import Path
import json

def get_stored_user_info(path):
    """获取存储的用户信息（如果有的话）"""
    if path.exists():
        contents = path.read_text()
        user_dict = json.loads(contents)
        return user_dict
    else:
        return None

def get_user_info(path):
    """获取用户数据"""
    user_name = input("我该怎么称呼您：")
    user_f_num = input("您喜欢的数字是：")
    user_f_animal = input("您喜欢的动物是：")
    user_dict = {
        'user_name':user_name,
        'user_f_num':user_f_num,
        'user_f_animal':user_f_animal,
    }
    contents = json.dumps(user_dict)
    path.write_text(contents)
    return user_dict

def greet_user():
    """问候用户"""
    path = Path('user_info.json')
    user_dict = get_stored_user_info(path)
    if user_dict:
        print(f"Welcome back, {user_dict['user_name']}!")
        print(f"I remember your favorite number is {user_dict['user_f_num']}. ")
        print(f"Have you seen a {user_dict['user_f_animal']} recently?")
    else:
        user_dict = get_user_info(path)
        msg = f"We'll remember you when you return, {user_dict['user_name']}!"
        print(msg)

greet_user()

